/*
 * Copyright (c) 2001, Aslak Hellesøy, BEKK Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of BEKK Consulting nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package middlegen.plugins.entitybean.swing;

import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import middlegen.Table;
import middlegen.plugins.entitybean.CMP30Table;
import middlegen.swing.JTableSettingsPanel;

/**
 * <Describe class>
 *
 * @author <a href="mailto:eivind.waaler@bekk.no">Eivind Waaler</a>
 * @created 7. november 2005
 * @version $Id: JEntity30TableSettingsPanel.java,v 1.4 2005/11/13 18:19:30 eivindw Exp $
 */
public class JEntity30TableSettingsPanel extends JTableSettingsPanel {

   /**
    * @todo-javadoc Describe the field
    */
   private ActionListener _pkListener =
      new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
            _currentTable.setPkGeneratorType((String) _pkGeneratorTypeComboBox.getSelectedItem());
            enableGeneratorName();
         }
      };

   /**
    * @todo-javadoc Describe the field
    */
   private final DocumentListener _documentListener =
      new DocumentListener() {
         public void changedUpdate(DocumentEvent event) {
            update();
         }


         public void insertUpdate(DocumentEvent event) {
            update();
         }


         public void removeUpdate(DocumentEvent event) {
            update();
         }


         private void update() {
            _currentTable.setPkGeneratorName(_pkGeneratorNameField.getText());
         }
      };

   /**
    * @todo-javadoc Describe the field
    */
   private CMP30Table _currentTable;

   /**
    * @todo-javadoc Describe the field
    */
   private final JLabel _pkGeneratorTypeLabel = new JLabel("PK Generator Type");

   /**
    * @todo-javadoc Describe the field
    */
   private final DefaultComboBoxModel _pkGeneratorTypeComboModel = new DefaultComboBoxModel();

   /**
    * @todo-javadoc Describe the field
    */
   private final JComboBox _pkGeneratorTypeComboBox = new JComboBox(_pkGeneratorTypeComboModel);

   /**
    * @todo-javadoc Describe the field
    */
   private final JLabel _pkGeneratorNameLabel = new JLabel("PK Generator Name");

   /**
    * @todo-javadoc Describe the field
    */
   private JTextField _pkGeneratorNameField = new JTextField();

   /**
    * @todo-javadoc Describe the field
    */
   private JPanel _pkPanel;

   /** {@inheritDoc} */
   public JEntity30TableSettingsPanel() {
      super();

      _pkPanel = new JPanel();
      _pkPanel.setLayout(new GridLayout(0, 2, 10, 5));
      _pkPanel.setBorder(BorderFactory.createTitledBorder("Primary key settings"));

      _pkPanel.add(_pkGeneratorTypeLabel);
      _pkGeneratorTypeComboModel.removeAllElements();
      _pkGeneratorTypeComboModel.addElement("NONE");
      _pkGeneratorTypeComboModel.addElement("IDENTITY");
      _pkGeneratorTypeComboModel.addElement("SEQUENCE");
      _pkGeneratorTypeComboModel.addElement("TABLE");
      _pkGeneratorTypeComboModel.addElement("AUTO");
      _pkGeneratorTypeComboBox.addActionListener(_pkListener);

      _pkPanel.add(_pkGeneratorTypeComboBox);

      _pkPanel.add(_pkGeneratorNameLabel);
      _pkGeneratorNameField.getDocument().addDocumentListener(_documentListener);
      _pkPanel.add(_pkGeneratorNameField);

      add(_pkPanel);
   }

   /**
    * {@inheritDoc}
    *
    * @param table The new Table value.
    */
   public void setTable(Table table) {
      _currentTable = (CMP30Table) table;

      if (_currentTable.isCompositeKey()) {
         _pkPanel.setVisible(false);
      }
      else {
         _pkPanel.setVisible(true);

         _pkGeneratorTypeComboModel.setSelectedItem(_currentTable.getPkGeneratorType());
         _pkGeneratorNameField.setText(_currentTable.getPkGeneratorName());
         enableGeneratorName();
      }
   }

   /**
    * Describe what the method does
    *
    * @todo-javadoc Write javadocs for method
    */
   private void enableGeneratorName() {
      if (_currentTable.getPkGeneratorType().equals("NONE") || _currentTable.getPkGeneratorType().equals("AUTO")) {
         _pkGeneratorNameField.setText("");
         _pkGeneratorNameField.setEditable(false);
      }
      else {
         _pkGeneratorNameField.setEditable(true);
      }
   }
}
